#include "GINFOProcessor.h"
#include <mastring.h>
#include <mastdlib.h>
#include <matime.h>
#include "conprint.h"
#include "../std.h"
#include "../Utilities/masscanf.h"
#include "../Utilities/trim.h"
#include "../Utilities/SoapHelper.h"

GINFOProcessor::GINFOProcessor(){}

GINFOProcessor::~GINFOProcessor(){}

//XmlListener
void GINFOProcessor::mtxEncoding(const char* value)
{

}
void GINFOProcessor::mtxTagStart(const char* name, int len){

	if (mFirstTag.length()==0){
		mFirstTag.append(name, len);
		mInBody = false;
		mInItem = false;
		return;
	}
	if (strncmp(name, "soap:Body", 9)==0){
		mInBody = true;
		return;
	}
	if (strncmp(name, "bridgeDetailResponse",20) == 0){
		mGINFOTag == GINFO_NULL;
		mInItem = true;
		mCurrentItem.result.clear();
		return;
	}else if (strncmp(name, "bridgeDetailResult", 18 ) ==0){
		mGINFOTag = GINFO_RESULT;
		return;
	}
}

void GINFOProcessor::mtxTagAttr(const char* attrName, const char* attrValue){

}
void GINFOProcessor::mtxTagStartEnd(){
	if (mFirstTag.length()== 0){
		mListener->processingComplete();
	}
}
void GINFOProcessor::mtxTagData(const char* data, int len){
	temp.append(data, len);
}
void GINFOProcessor::mtxTagEnd(const char* name, int len){
	//trim(temp);
	//parseControlCodes(temp);
	//stripHTML(temp);

	switch(mGINFOTag){
	case GINFO_NULL:
		break;
	case GINFO_RESULT:
		mCurrentItem.result = temp;
		break;
	}

	temp.clear();

	mGINFOTag = GINFO_NULL;

	if (strncmp(name, "bridgeDetailResult", 18)==0){
		mInItem = false;
		if (mListener != NULL){
			mListener->itemReceived(mCurrentItem);
		}
	}

	if(strcmp(name, "soap:Body") == 0) {
			mInBody = false;
		}

	if (mFirstTag == name){
		mFirstTag.clear();
		mListener->processingComplete();
	}
}

void GINFOProcessor::mtxParseError(){

}
void GINFOProcessor::mtxEmptyTagEnd(){

}
unsigned char GINFOProcessor::mtxUnicodeCharacter(int unicode){
	/*char c = XmlListener::mtxUnicodeCharacter(unicode);
	if (c == 0){
		c = '?';
	}
	return c;*/
	char c = getUnicodeCharacter(unicode);
	return c;
}

//Listener
void GINFOProcessor::setGINFOListener(GINFOListener* listener){
	mListener = listener;
}

void GINFOProcessor::addRequest(String& url, GINFOListener* requester){
	if (APPCONTROLLER->getConnection()->isBusy()) return;

	APPCONTROLLER->getConnection()->setURL(url.c_str());
	mListener = requester;
	lprintfln("begin GINFO download");
	APPCONTROLLER->getConnection()->beginDownloading();
}

void GINFOProcessor::setGermId(String& value){
	mGermId = value;
	String sSoap = SH_LotBridgeDetail(mGermId);
	int sSoapLen = sSoap.length();
	String sSoapLenStr = integerToString(sSoapLen);
	String sLength = "Content-Length";
	String sType = "Content-Type";
	String sTypeValue = "text/xml;charset=UTF-8";
	APPCONTROLLER->getConnection()->clearHeaders();
	APPCONTROLLER->getConnection()->setHeader(sLength, sSoapLenStr);
	APPCONTROLLER->getConnection()->setHeader(sType, sTypeValue);
	APPCONTROLLER->getConnection()->setData(sSoap);
	APPCONTROLLER->getConnection()->setXmlListener(this);
}
